package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHKetqua;
import com.lb.jwitter.service.TblDKMHKetquaService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;
import com.lb.jwitter.service.TblThongTinTKBChiTiet;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.KiemTraDangKy;
import com.lb.jwitter.util.Message;
import com.lb.jwitter.util.QuyenDangKy;
import com.lb.jwitter.util.ThoiKhoaBieuDuKien;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class HieuChinhKetquaDangKyAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private Long ketquaId;
	public Long getKetquaId() {
		return ketquaId;
	}

	public void setKetquaId(Long ketquaId) {
		this.ketquaId = ketquaId;
	}

	private Long nlmhId;



	public Long getNlmhId() {
		return nlmhId;
	}

	public void setNlmhId(Long nlmhId) {
		this.nlmhId = nlmhId;
	}

	private List<ThoiKhoaBieuDuKien> thoiKhoaBieuDuKiens;


	public List<ThoiKhoaBieuDuKien> getThoiKhoaBieuDuKiens() {
		return thoiKhoaBieuDuKiens;
	}

	public void setThoiKhoaBieuDuKiens(List<ThoiKhoaBieuDuKien> thoiKhoaBieuDuKiens) {
		this.thoiKhoaBieuDuKiens = thoiKhoaBieuDuKiens;
	}

	
	private int soTietTrongNgay;

	public int getSoTietTrongNgay() {
		return soTietTrongNgay;
	}

	private TblCTDTMonHoc tblCTDTMonHoc;
	public TblCTDTMonHoc getTblCTDTMonHoc() {
		return tblCTDTMonHoc;
	}

	public void setTblCTDTMonHoc(TblCTDTMonHoc tblCTDTMonHoc) {
		this.tblCTDTMonHoc = tblCTDTMonHoc;
	}

	public void setSoTietTrongNgay(int soTietTrongNgay) {
		this.soTietTrongNgay = soTietTrongNgay;
	}
	
	private TblCTDTDMTietHocService tblCTDTDMTietHocService;
	
	private Boolean trongHanDK;

	public Boolean getTrongHanDK() {
		return trongHanDK;
	}

	public void setTrongHanDK(Boolean trongHanDK) {
		this.trongHanDK = trongHanDK;
	}
	
	private     KetQuaDangKy ketQuaDangKy = null;

	public KetQuaDangKy getKetQuaDangKy() {
		return ketQuaDangKy;
	}

	public void setKetQuaDangKy(KetQuaDangKy ketQuaDangKy) {
		this.ketQuaDangKy = ketQuaDangKy;
	}

	private String msg;
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	private String code;
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	    ApplicationContext context = contextProvider.getApplicationContext(); 
	    
	    TblThongTinNhomLopMonHocService tblThongTinNhomLopMonHocService= (TblThongTinNhomLopMonHocService)  context.getBean("tbl_ThongTin_NhomLop_MonHocClient");
	    tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context.getBean("tbl_CTDT_DM_TietHocClient");
	    TblThongTinTKBChiTietService tblThongTinTKBChiTietService = (TblThongTinTKBChiTietService)  context.getBean("tbl_ThongTin_TKB_ChiTietClient");
		
	    TblDKMHKetquaService tblDKMHKetquaService = (TblDKMHKetquaService) context.getBean("tbl_DKMH_KetquaClient");
	    
	    
	    List<KetQuaDangKy> ketQuaDangKys = (List<KetQuaDangKy>) session.get("ketQuaDangKys");
	    
	    trongHanDK = (Boolean) session.get("trongHanDK");
	    QuyenDangKy quyenDK = (QuyenDangKy) session.get("quyenDK");
	    
	    if(trongHanDK && quyenDK.getUpdate()){
	    	for(KetQuaDangKy ketqua : ketQuaDangKys){
	    		if(    ketqua.getKetqua().getId().equals(ketquaId)  && ketqua.getKetqua().getKhoaMonHocYN().equals("N")){
	
					if(!ketqua.getKetqua().getNLMHId().getId().equals(new Long(nlmhId))){
		
					    TblThongTinNhomLopMonHoc tblThongTinNhomLopMonHoc = tblThongTinNhomLopMonHocService.gettblThongTinNhomLopMonHoc(new Long(nlmhId));
					    if(tblThongTinNhomLopMonHoc != null){

					    	ThoiKhoaBieuDuKien thoiKhoaBieuDuKien = new ThoiKhoaBieuDuKien();
					    	thoiKhoaBieuDuKien.setNhomLop(tblThongTinNhomLopMonHoc);
					    	
					    	List<ConditionReport> cons = new ArrayList<ConditionReport>();
						    { 
						        ConditionReport con = new ConditionReport(); 
						        con.setCol("TKBId.id");
						        con.setValue1(tblThongTinNhomLopMonHoc.getTKBId().getId());
						        con.setOperator("eq");
						        cons.add(con);	
						    }
						    { 
						        ConditionReport con = new ConditionReport(); 
						        con.setCol("ngayHocId.STT");
						        con.setValue1("asc");
						        con.setOperator("order");
						        cons.add(con);	
						    }
					    	thoiKhoaBieuDuKien.setChiTiets(tblThongTinTKBChiTietService.findtblThongTinTKBChiTietByCondition(cons, 0, 0));
					    	
					    	//kiem tra trung TKB
					    	//trung thoi khoa bieu
//						    for(KetQuaDangKy ketQuaDangKy : ketQuaDangKys){
//						    	if(!ketQuaDangKy.getKetqua().getId().equals(ketquaId)){
//						    		if(ketQuaDangKy.getKetqua().getMaDieuChinh().equals("BS") || ketQuaDangKy.getKetqua().getMaDieuChinh().equals("HC") ){
//									    
//								    	for(TblThongTinTKBChiTiet chiTietDaDK : ketQuaDangKy.getThoiKhoaBieuDuKien().getChiTiets()){
//								    		for(TblThongTinTKBChiTiet chiTietDK : thoiKhoaBieuDuKien.getChiTiets()){
//								    			
//									    		if(chiTietDaDK.getNgayHocId().getId().equals(chiTietDK.getNgayHocId().getId())){
//									    			
//									    			int tietKetThucDaDK = chiTietDaDK.getTietBatDauId().getSTT() + chiTietDaDK.getSoTietHoc() - 1 ;
//									    			int tietKetThucDK = chiTietDK.getTietBatDauId().getSTT() + chiTietDK.getSoTietHoc() - 1;
//									    			
//									    			if((chiTietDK.getTietBatDauId().getSTT()>= chiTietDaDK.getTietBatDauId().getSTT() && chiTietDK.getTietBatDauId().getSTT() <= tietKetThucDaDK)
//									    					|| (tietKetThucDK >= chiTietDaDK.getTietBatDauId().getSTT() && tietKetThucDK <= tietKetThucDaDK)
//									    					|| (chiTietDK.getTietBatDauId().getSTT()<= chiTietDaDK.getTietBatDauId().getSTT() && tietKetThucDK >= tietKetThucDaDK)){
//									    				
//									    				code = "ERROR";
//									    				msg = "M\u00F4n ho\u0323c bi\u0323 tru\u0300ng th\u01A1\u0300i kho\u0301a bi\u00EA\u0309u!";
//									    				
//									    				return Action.SUCCESS;
//									    			}//end if
//									    		}//end if
//									    	}//end for
//								    	}
//						    		}
//						    	}
//						    }
					    	KetQuaDangKy dangKyMoi = new KetQuaDangKy();
					    	TblDKMHKetqua kq = new TblDKMHKetqua();
					    	kq.setDotDKId(ketqua.getKetqua().getDotDKId());
					    	kq.setKetQua(ketqua.getKetqua().getKetQua());
					    	kq.setKetQuaLyDo(ketqua.getKetqua().getKetQuaLyDo());
					    	kq.setKetQua2(ketqua.getKetqua().getKetQua2());
					    	kq.setKetQuaLyDo2(ketqua.getKetqua().getKetQuaLyDo2());
					    	kq.setHocVienId(ketqua.getKetqua().getHocVienId());
					    	kq.setId(ketqua.getKetqua().getId());
					    	kq.setKhoaMonHocYN(ketqua.getKetqua().getKhoaMonHocYN());
					    	kq.setMaDieuChinh(ketqua.getKetqua().getMaDieuChinh());
					    	kq.setNLMHId(tblThongTinNhomLopMonHoc);
					    	kq.setTinhTrang(ketqua.getKetqua().getTinhTrang());
					    	kq.setXacNhanYN(ketqua.getKetqua().isXacNhanYN());
			
					    	dangKyMoi.setKetqua(kq);
					    	dangKyMoi.setThoiKhoaBieuDuKien(thoiKhoaBieuDuKien);
					    	
					    	// --------------------------------------------------------------------------
							// kiem tra si so hoc vien
					    	{
								Message message = new Message();
								ChumanoLogger.log("[KIEM TRA SI SO LOP");
								if (KiemTraDangKy.kiemTraVuotSiSoNhomLop(dangKyMoi, message)) {
									if(message.getCode()!=null && message.getCode().equals("NOTICE1")){
										dangKyMoi.getKetqua().setKetQua("+");
										dangKyMoi.getKetqua().setKetQuaLyDo( message.getMsg());
											// tra ve ket qua notice
											code = message.getCode();
											// notice
											msg = message.getMsg();
										
									}else if(message.getCode()!=null && message.getCode().equals("NOTICE2")){
										dangKyMoi.getKetqua().setKetQua("?");
										dangKyMoi.getKetqua().setKetQuaLyDo( message.getMsg());
											// tra ve ket qua notice
											code = message.getCode();
											// notice
											msg = message.getMsg();
										
									}else {
									code = "ERROR";
									// qua si so lop cho phep
									msg = "Quá sĩ số lớp cho phép!";
									ChumanoLogger.log(msg);
									ChumanoLogger.stop();
									return Action.ERROR;
									}
								}
					    	}

							// ---------------------------------------------------------------------
							// kiem tra hedao tao nhom lop
							{
								ChumanoLogger.log("[KIEM TRA HE DAO TAO - NHOM LOP");
								Message message = new Message();
								if (KiemTraDangKy.kiemTraNhomLopKhongDuocDangKy(dangKyMoi,context, message)) {
									code = "ERROR";
									msg = message.getMsg();
								
									return Action.ERROR;
								}
							}

							// ----------------------------------------------------------------------
							// kiem tra ngoai le nhom VIet-Phap
							// dieu kien dau vao: kiem tra sau khi kiem tra hedaotao-nhomlop
							{
								ChumanoLogger.log("[KIEM TRA VIET-PHAP");
								Message message = new Message();
								if (KiemTraDangKy.kiemTraNgoaiLe(dangKyMoi, ketQuaDangKys,message)) {
									code = "ERROR";
									msg = message.getMsg();
									
									return Action.ERROR;
								}
							}
					    	//----------------------------------
					    	//kiem tra trung TKB
							{
								Message message = new Message();
						    	boolean trungTKB = KiemTraDangKy.kiemTraTrungTKB(dangKyMoi, ketQuaDangKys,message);
				    			if(trungTKB){
				    				code = "ERROR";
				    				msg = "Môn học bị trùng thời khóa biểu!";
				    				
				    				return Action.ERROR;
				    			}
							}
						    
			    			//thoa cac Dk rang buoc
						    //cap nhat NLMH
			    			if(ketqua.getKetqua().isXacNhanYN() && ketqua.getKetqua().getKetQua().equals("*")){
			    				ketqua.getKetqua().setMaDieuChinh2(ketqua.getKetqua().getNLMHId().getId().toString());
			    			}
					    	ketqua.getKetqua().setNLMHId(tblThongTinNhomLopMonHoc);
					    	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
		    			    Date dd = new Date(); 
		    			    GregorianCalendar c = new GregorianCalendar(); 
		    			    c.setTime(dd);
		    			    XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
		    				ketqua.getKetqua().setNgayCapNhat(d);
		    				
		    				ketqua.getKetqua().setKetQua(kq.getKetQua2());
		    				ketqua.getKetqua().setKetQuaLyDo(kq.getKetQuaLyDo2());
		    				if(ketqua.getKetqua().isXacNhanYN()){

						    	ketqua.getKetqua().setKetQua2(kq.getKetQua());
						    	ketqua.getKetqua().setKetQuaLyDo2(kq.getKetQuaLyDo());
		    				}
		    				ketqua.getKetqua().setMaDieuChinh("HC");
					    	ketqua.getKetqua().setXacNhanYN(false);
					    	ketqua.setThoiKhoaBieuDuKien(thoiKhoaBieuDuKien);
					    
					    	tblDKMHKetquaService.updatetblDKMHKetqua(ketqua.getKetqua());
					    	
					        code = "SUCCESS";
							msg = "Hiệu chỉnh thành công!!!";
					    }//end if
					}//end if
	    		}//end if
	    	}//end for
	    }else{
	    	code = "ERROR";
	    	msg = "Đã hết hạn hiệu chỉnh!!!";
	    	
	    	return Action.ERROR;
	    }
		    session.put("ketQuaDangKys", ketQuaDangKys);
		    
		
			
		    return Action.SUCCESS;

	}


	
	
	
	
	
}
